/**
 * Main.java
 * 
 * Created On: Nov 18, 2008
 * Created By: ## CHANGE IN JAVA->CODE STYLE->CODE TEMPLATES->New Java Files
 *
 * Copyright (C) 2008 University of Wisconsin Platteville - ResNet
 *
 * All rights are reserved to the University of Wisconsin Platteville
 */

package view;

import java.io.IOException;

import model.Dialog;
import model.Language;
import model.TranslationService;
import model.TranslatedText;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;

import controller.Translator;

public class SWT_UI implements SelectionListener, KeyListener
{
	public static final int TITLE_LABEL_HEIGHT = 50;
	
	public static final int TEXT_WIDTH = 400;
	public static final int TEXT_HEIGHT = 250;
	
	private Shell shell;
	
	private Button translateButton;
	private Button copyButton;
	private Button exitButton;
	
	private Label titleLabel;
	private Label inputTextLabel;
	private Label outputTextLabel;
	
	private Text inputText;
	private Text outputText;
	
	public SWT_UI( Shell shell )
	{
		this.shell = shell;
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 20;
		layout.marginWidth = 20;
		shell.setLayout( layout );
		
		FontData fontData = new FontData();
		GridData layoutData = new GridData();
		
		titleLabel = new Label( shell, SWT.NONE );
		titleLabel.setText("Translation Tool");
		fontData.height = 16;
		fontData.style = SWT.BOLD;
		Font font = new Font( shell.getDisplay(), fontData );
		titleLabel.setFont(font);
		layoutData = new GridData( GridData.HORIZONTAL_ALIGN_CENTER );
		layoutData.horizontalSpan = 2;
		layoutData.heightHint = TITLE_LABEL_HEIGHT;
		titleLabel.setLayoutData(layoutData);
		
		inputTextLabel = new Label( shell, SWT.NONE );
		inputTextLabel.setText("Input Text:");
		
		outputTextLabel = new Label( shell, SWT.NONE );
		outputTextLabel.setText("Output Text:");
		
		inputText = new Text( shell, SWT.MULTI );
		inputText.addKeyListener( this );
		layoutData = new GridData();
		layoutData.widthHint = TEXT_WIDTH;
		layoutData.heightHint = TEXT_HEIGHT;
		inputText.setLayoutData(layoutData);
		
		outputText = new Text( shell, SWT.MULTI | SWT.READ_ONLY);
		
		layoutData = new GridData();
		layoutData.widthHint = TEXT_WIDTH;
		layoutData.heightHint = TEXT_HEIGHT;
		outputText.setLayoutData(layoutData);
		
		translateButton = new Button( shell, SWT.PUSH );
		translateButton.setText("Translate");
		translateButton.addSelectionListener( this );
		layoutData = new GridData( GridData.HORIZONTAL_ALIGN_END );
		layoutData.horizontalSpan = 2;
		translateButton.setLayoutData(layoutData);
		
		copyButton = new Button( shell, SWT.PUSH );
		copyButton.setText("Copy");
		copyButton.addSelectionListener( this );
		layoutData = new GridData( GridData.HORIZONTAL_ALIGN_END );
		layoutData.horizontalSpan = 2;
		copyButton.setLayoutData(layoutData);
		
		exitButton = new Button( shell, SWT.PUSH );
		exitButton.setText("Exit");
		exitButton.addSelectionListener( this );
		layoutData = new GridData( GridData.HORIZONTAL_ALIGN_END );
		layoutData.horizontalSpan = 2;
		exitButton.setLayoutData(layoutData);
		
		shell.setDefaultButton( translateButton );
	}
	
	public void widgetDefaultSelected( SelectionEvent widget )
   {
		System.out.println("Widget default selected");
	}

	public void widgetSelected( SelectionEvent widget )
	{
		if ( widget.getSource() == exitButton )
			end();
		else if ( widget.getSource() == translateButton )
			translate();
		else if ( widget.getSource() == copyButton )
			copyOutputText();
	}
	
	public void keyPressed( KeyEvent event )
	{
		
	}
	
	public void keyReleased( KeyEvent event )
	{
		if ( event.getSource() == inputText && event.stateMask == SWT.SHIFT 
				&& ( event.keyCode == SWT.CR || event.keyCode == SWT.LF ) )
		{
			inputText.setText( inputText.getText().replaceAll("\\s$", "") );
			inputText.selectAll();
			inputText.redraw();
			translate();
		}
	}
	
	private void translate()
	{
		Language fromLanguage = Language.EN;
		Language toLanguage = Language.DE;
		Dialog original = new Dialog( inputText.getText(), fromLanguage );
		try
		{
			TranslatedText translated = Translator.execute( 
					TranslationService.GOOGLE, original, toLanguage);
			outputText.setText( translated.getTranslatedText() );
			System.out.println( translated.getTranslatedText() );
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
	}
	
	private void copyOutputText()
	{
		String text = outputText.getText();
		TextTransfer textTransfer = TextTransfer.getInstance();
		Clipboard clipboard = new Clipboard( shell.getDisplay() );
		clipboard.setContents( new String[]{ text }, 
				new Transfer[]{ textTransfer } );
		clipboard.dispose();
	}
	
	private void end()
	{
		System.out.println( "Program Terminiation Successful" );
		System.exit(0);
	}

	public static final void center( Shell shell )
	{
		Display display = shell.getDisplay();
		Monitor primaryMonitor = display.getPrimaryMonitor();
		Rectangle parentBounds = primaryMonitor.getBounds();
		Rectangle childBounds = shell.getBounds();
		int x = ( parentBounds.width - childBounds.width ) / 2;
		int y = ( parentBounds.height - childBounds.height ) / 2;
		
		shell.setLocation( x, y );
	}
	
	public static final void main( String[] args )
	{
		Display.setAppName( "Translator" );
		Display display = new Display();
		
		Shell shell = new Shell( display );
		
		new SWT_UI( shell );
		
		shell.pack();
		
		SWT_UI.center( shell );
		
		shell.open();
		while ( !shell.isDisposed() )
		{
			if ( !display.readAndDispatch() )
				display.sleep();
		}
		shell.dispose();
	}
}
